<!DOCTYPE html>
<!--[if IE 8]>
<html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]>
<html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<!-- BEGIN HEAD -->

<head>
    <meta charset="utf-8"/>
    <title>添加书籍</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1" name="viewport"/>
    <meta content="" name="description"/>
    <meta content="" name="author"/>
    <meta http-equiv="Access-Control-Allow-Origin" content="*" />
    <link href="http://tv.speechb.com/lib/layui/css/layui.css" rel="stylesheet"/>
    <link href="http://tv.speechb.com/lib/superui/content/ui/global/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <style>
       .layui-required:after{
            content:"*";
            color:red;
            position: absolute;
            top:13px;
            left:15px;
        }
    </style>
</head>
<body>
<!--
<script id="editor" type="text/plain" style="height: 150px;"/>

-->
<!-- Main content -->
<div style="margin-top: 30px;margin-right: 22px">
    <div class="layui-form" action="" style="max-width: 640px;margin: 0 auto">

        <div class="layui-form-item">
            <label class="layui-form-label layui-required">书籍名称</label>
            <div class="layui-input-block">
                <input id="name" type="text" name="name" required lay-verify="required" placeholder="请输入书籍名称"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required">作者</label>
            <div class="layui-input-inline">
                <input id="author" type="text" name="author" required lay-verify="required" placeholder="请输入作者名称" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">书籍作者</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required">页码</label>
            <div class="layui-input-inline">
                <input id="pager_count" type="text" name="pager_count" required lay-verify="required" placeholder="请输入页码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">书籍总页数</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-required">阅读链接</label>
            <div class="layui-input-inline">
                <input id="download_url" type="text" name="download_url" required lay-verify="required" placeholder="请输入在线阅读链接" autocomplete="off" class="layui-input">
            </div>
            <div> <button id="bookUploadBtn" type="button" style="margin-top: 5px" class="layui-btn layui-btn-sm layui-btn-normal fa fa-folder-open-o"> 书籍上传</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-required">封面链接</label>
            <div class="layui-input-inline">
                <input id="cover_url" type="text" name="cover_url" required lay-verify="required" placeholder="请输入书籍展示封面" autocomplete="off" class="layui-input">
            </div>
            <div>
                <button id="bookCoverBtn" type="button" style="margin-top: 5px" class="layui-btn layui-btn-sm layui-btn-normal fa fa-folder-open-o"> 封面上传</button>
            </div>
        </div>
        <img id="cover_img" style="height: 160px;margin-left: 100px;margin-bottom:10px;border: #a0a0a0;display: none;" src="https://tv.speechb.com/static/hold_girl_book/lianaitouzixue/1.jpg">
        <div class="layui-form-item">
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">书籍描述</label>
                <div class="layui-input-block">
                    <textarea id='book_desc' rows="6" name="book_desc" style="width:100%;max-height: 220px;overflow:auto;"
                              class="layui-textarea"></textarea>
                    <br/>

                </div>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button id="submitBtn" class="layui-btn" lay-submit lay-filter="formDemo" style="margin: 0 auto">立即提交
                </button>
            </div>
        </div>
    </div>
</div>
<input type="file" style="display:none;" id="fileInput" name="files" >
<input type="file" style="display:none;" id="coverFileInput" name="files" >
<script type="text/javascript" charset="utf-8" src="http://tv.speechb.com/lib/layui/layui.js"></script>
<script src="http://tv.speechb.com/lib/superui/content/ui/global/jQuery/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="http://tv.speechb.com/lib/toaster/jquery.toaster.js"></script>
<script type="text/javascript" src="/static/admin/js/publish_com.js"></script>
<script type="text/javascript">
    var phoneSign;
    var resourceSign;

    //点击本地上传文件
    $('#bookUploadBtn').click( () => {
        $('#fileInput').click();
    });
    $('#bookCoverBtn').click( () => {
        $('#coverFileInput').click();
    });


    $('#fileInput').change( (event) => {
        var files = event.target.files;
        uploadPdf(files[0]);
    });
    $('#coverFileInput').change( (event) => {
        var files = event.target.files;
        uploadPdfCover(files[0]);
    });
    layui.use('form', function () {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            $("#submitBtn").attr("disabled", "disabled")
            var url = "/admin/hold_girl_book/publish";
            if (id != null) {
                url = "/admin/hold_girl_book/book-update";
            }
            $.ajax({
                type: "POST",
                url: url,
                data: {
                    "name": data.field.name,
                    "author": data.field.author,
                    "pager_count": data.field.pager_count,
                    "download_url":data.field.download_url,
                    "cover_url":data.field.cover_url,
                    "book_desc":data.field.book_desc,
                    "id": id
                },
                success: function (result) {
                    $("#activation_code").html("");
                    if (result.code == 1) {
                        layer.msg('添加成功！', {icon: 1});
                        $('#name').val("");
                        $('#author').val("");
                        $('#pager_count').val("");
                        $('#cover_url').val("");
                        $('#book_desc').val("");
                    } else {
                        layer.msg('生成失败了！', {icon: 5});
                    }
                    $("#submitBtn").removeAttr("disabled");
                },
                error: function (e) {
                    layer.msg('请求异常！', {icon: 5});
                    $("#submitBtn").removeAttr("disabled");
                }
            });
            return false;
        });
    });
    $(function () {
        function getSign() {
            $.ajax({
                type : "GET",
                url : "/admin/resource_sign",
                success : function(result) {
                    if(result.code==1) {
                        phoneSign = result.data.sign;
                        resourceSign = result.data.jwt;
                        $.toaster({ priority : "success", title : "通知", message : "获取签证成功!" });
                    } else {
                        $.toaster({ priority : "danger", title : "通知", message : "获取签证失败!" });
                    }
                },
                error : function(e){
                    $.toaster({ priority : "danger", title : "通知", message : "获取签证失败!" });
                }
            });
        }
        getSign();
    });
    /*文件上传,单个文件上传*/
    function uploadPdf(file) {
        var form = new FormData();
        if(phoneSign==null || resourceSign==null) {
            $.toaster({ priority : "warning", title : "通知", message : "签证有误，正在重新获取!" });
            getSign();
            return;
        }
        form.append("files",file);
        form.append("phone_sign",phoneSign);
        form.append("resource_sign",resourceSign);
        $.ajax({
            url:'http://www.tv.speechb.com/magnetw/upload_pdf',
            //url:'http://localhost:8080/bt/upload_pdf',
            type:'POST',
            cache:false,
            contentType:"multipart/form-data",
            data:form,
            processData:false,
            contentType:false,
            xhr:function(){
                myXhr = $.ajaxSettings.xhr();

                return myXhr;
            },
            success:function(data){
                if(data.code =="1" ){
                    $.toaster({ priority : "success", title : "通知", message : "书籍上传成功" });
                    var pdf = data.data[0];
                    $("#download_url").val('/'+pdf.url);
                    $("#pager_count").val(pdf.pageCount);
                    var title = pdf.title;
                    if (title != null) {
                        $("#name").val(title);
                    } else {
                        $("#name").val("");
                    }
                    var author = pdf.author;
                    if (author != null) {
                        $("#author").val(author);
                    } else {
                        $("#author").val("");
                    }
                } else {
                    $.toaster({ priority : "danger", title : "通知", message : "书籍上传失败" });
                }
            },
            error:function(){
                $.toaster({ priority : "danger", title : "通知", message : "书籍上传失败" });
            }
        });
    }

    /*文件上传,单个文件上传*/
    function uploadPdfCover(file) {
        $("#cover_img").hide();
        var form = new FormData();
        if(phoneSign==null || resourceSign==null) {
            $.toaster({ priority : "warning", title : "通知", message : "签证有误，正在重新获取!" });
            getSign();
            return;
        }
        form.append("files",file);
        form.append("phone_sign",phoneSign);
        form.append("resource_sign",resourceSign);
        form.append("url_part", "book/conver");
        $.ajax({
            url:'http://www.tv.speechb.com/magnetw/upload_img',
            //url:'http://localhost:8080/bt/upload_img',
            type:'POST',
            cache:false,
            contentType:"multipart/form-data",
            data:form,
            processData:false,
            contentType:false,
            xhr:function(){
                myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },
            success:function(data){
                if(data.code =="1" ){
                    $.toaster({ priority : "success", title : "通知", message : "书籍封面上传成功" });
                    $("#cover_url").val('/'+data.data[0]);
                    $("#cover_img").attr("src","http://www.tv.speechb.com/" +data.data[0]);
                    $("#cover_img").show();
                } else {
                    $.toaster({ priority : "danger", title : "通知", message : "书籍封面上传失败" });
                }
            },
            error:function(){
                $.toaster({ priority : "danger", title : "通知", message : "书籍封面上传失败" });
            }
        });
    }

    dealUpdate("/admin/hold_girl_book/update_inf", function (result) {
        $("#name").val(result.data.name);
        $("#author").val(result.data.author);
        $("#pager_count").val(result.data.pager_count);
        $("#download_url").val(result.data.download_url);
        $("#cover_url").val(result.data.cover_url);
        $("#book_desc").val(result.data.book_desc);
    });
</script>
</body>

</html>